﻿using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Extensions;
using FytSoa.Service.Interfaces;
using SqlSugar;
namespace FytSoa.Service.Implements
{
    /*!
    * 文件名称：Sw_activitygood服务接口实现
    */
    public class Sw_activitygoodService : BaseServer<Sw_activitygood>, ISw_activitygoodService
    {
        /// <summary>
        /// 关联商品和取消关联操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> ActivityLinkGoods(Sw_activitygood model)
        {
            var res = new ApiResult<string>() { statusCode = (int)ApiEnum.Error };
            try
            {
                //判断是否存在
                if (!string.IsNullOrEmpty(model.Guid))
                {
                    //删除操作
                    await Db.Deleteable(model).ExecuteCommandAsync();
                }
                else
                {
                    //添加操作
                    model.Guid = System.Guid.NewGuid().ToString();
                    await Db.Insertable(model).ExecuteCommandAsync();
                }
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<SwActivityGoodsDto>>> GetPageGoods(PageParm param)
        {
            var res = new ApiResult<Page<SwActivityGoodsDto>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var query = Db.Queryable<Sw_goods, Sw_activitygood>((g, act) => new JoinQueryInfos(
                           JoinType.Left, g.Guid == act.GoodGuid
                       ));
                if (param.status==1)
                {
                    query = Db.Queryable<Sw_goods, Sw_activitygood>((g, act) => new JoinQueryInfos(
                           JoinType.Inner, g.Guid == act.GoodGuid
                       ));
                }
                res.data = await query
                    .WhereIF(!string.IsNullOrEmpty(param.key), (g, act) => g.GoodsName.Contains(param.key))
                    .WhereIF(!string.IsNullOrEmpty(param.where), "g."+param.where)
                    .WhereIF(param.id != 0, (g, act) => g.ColumnParentGuid.Contains("," + param.id.ToString() + ","))
                    .OrderBy((g, act) => act.Sort, OrderByType.Desc)
                    .Select((g, act)=>new SwActivityGoodsDto() {
                        Guid=g.Guid,
                        GoodsName=g.GoodsName,
                        SalePrice=g.SalePrice,
                        KcSum=g.KcSum,
                        IsAudit=g.IsAudit,
                        IsTop=g.IsTop,
                        IsRed=g.IsRed,
                        IsDelivery=g.IsDelivery,
                        IsDisCount=g.IsDisCount,
                        IsFenXiao=g.IsFenXiao,
                        IsDel=g.IsDel,
                        ActivityGoodsGuid=act.Guid,
                        ActivityGuid =act.ActivityGuid,
                        ActivityMoney=act.ActivityMoney,
                        Limits=act.Limits,
                        Sort=act.Sort
                    })
                    .ToPageAsync(param.page, param.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }
    }
}